package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.WeixinMsgReceiveDao;
import com.suyang.syt.dao.interfaces.WeixinUserDao;
import com.suyang.syt.model.WeixinMsgReceive;
import com.suyang.syt.model.WeixinUser;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.WeixinMsgReceiveDTO;
import com.suyang.syt.service.interfaces.WeixinMsgReceiveService;
import com.suyang.syt.util.EmptyUtil;

@Service
public class WeixinMsgReceiveImpl implements WeixinMsgReceiveService {

	@Autowired
	private WeixinMsgReceiveDao weixinMsgReceiveDao;
	@Autowired
	private WeixinUserDao weixinUserDao;

	@Override
	public WeixinMsgReceive add(WeixinMsgReceive receiveMsg) throws Exception {
		if (receiveMsg != null) {
			if (weixinMsgReceiveDao.countByCreateTime(receiveMsg.getCreateTime(), receiveMsg.getFromUserName()) <= 0) {
				weixinMsgReceiveDao.add(receiveMsg);
				return receiveMsg;
			}
		}
		return null;
	}

	@Override
	public PagerEasyUI<WeixinMsgReceiveDTO> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2) {
		StringBuilder hql = new StringBuilder(" from WeixinMsgReceive wrm where 1=1 ");
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and wrm.createTime >= '").append(createDate1).append("' ");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and wrm.createTime <= '").append(createDate2).append("' ");
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by wrm.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<WeixinMsgReceiveDTO> easyUIPager = new PagerEasyUI<WeixinMsgReceiveDTO>();
		List<WeixinMsgReceive> list = weixinMsgReceiveDao.findByEasyUI(hql.toString(), page, rows);
		if (EmptyUtil.isNotEmpty(list)) {
			List<WeixinMsgReceiveDTO> datas = new ArrayList<WeixinMsgReceiveDTO>();
			WeixinUser weixinUser = null;
			String openid = "";
			for (WeixinMsgReceive weixinMsgReceive : list) {
				if (EmptyUtil.isEmpty(openid) || !openid.equals(weixinMsgReceive.getFromUserName())) {
					weixinUser = weixinUserDao.loadByOpenid(weixinMsgReceive.getFromUserName());
				}
				openid = weixinMsgReceive.getFromUserName();
				datas.add(new WeixinMsgReceiveDTO(weixinMsgReceive, weixinUser));
			}
			easyUIPager.setRows(datas);
			easyUIPager.setTotal((Long) weixinMsgReceiveDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		}
		return easyUIPager;
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (EmptyUtil.isNotEmpty(ids)) {
			weixinMsgReceiveDao.deletes(ids);
			return new AjaxObj(1, "删除成功！");
		} else {
			return new AjaxObj(0, "请选择要删除的记录！");
		}
	}

}
